#!/bin/zsh

# WORKOUTS
# Shows historical workouts with target and actuals

USR=$1
EXERCISE=$2

set -eu

checkvars USR EXERCISE

[[ -d users/${USR} ]] || crash "No such user: ${USER}"

for PLAN in users/${USR}/plans/plan-*.txt
do
  NAME=${PLAN:t}
  [[ ${NAME} == plan-2012*    ]] && continue
  [[ ${NAME} == plan-2013-01* ]] && continue
  if grep -q "\[${EXERCISE}\]" ${PLAN}
  then
    [[ ${NAME} =~ "plan-(.*).txt" ]]
    printf "${match} "
    TARGET=$( grep "\[${EXERCISE}\].*target=.* " ${PLAN} | grep -o "target=.* " )
    WORKOUT=( $( grep -A 1 "\[${EXERCISE}\] " ${PLAN} | sed -n 2p ) )
    if [[ ${#WORKOUT} == 15 ]]
    then
      WORKOUT=( ${WORKOUT[1]} ${WORKOUT[3]}  ${WORKOUT[6]}
                ${WORKOUT[8]} ${WORKOUT[11]} ${WORKOUT[13]} )
    else
      WORKOUT=( ${WORKOUT[1]} ${WORKOUT[3]}  ${WORKOUT[5]}
                ${WORKOUT[7]} ${WORKOUT[9]} ${WORKOUT[11]} )
    fi
    printf "%-14s" ${TARGET}
    printf "%s x %-3s %s x %-3s %s x %-3s\n" ${WORKOUT}
  fi
done
